package com.dragon.manage.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dragon.manage.bean.ActivityInfo;
import com.dragon.manage.bean.ActivityOption;
import com.dragon.manage.bean.AmusementInfo;
import com.dragon.manage.bean.ManageUser;
import com.dragon.manage.bean.MatchInfo;
import com.dragon.manage.bean.UserActivitybet;
import com.dragon.manage.bean.UserBet;
import com.dragon.manage.bean.UserInfo;
import com.dragon.manage.dao.IActivityInfoDAO;
import com.dragon.manage.dao.IActivityOptionDAO;
import com.dragon.manage.dao.IAmusementInfoDAO;
import com.dragon.manage.dao.IAmusementTypeDAO;
import com.dragon.manage.dao.ICacheDao;
import com.dragon.manage.dao.IManageUserDAO;
import com.dragon.manage.dao.IMatchInfoDAO;
import com.dragon.manage.dao.ITeamInfoDAO;
import com.dragon.manage.dao.IUserActivitybetDAO;
import com.dragon.manage.dao.IUserBetDAO;
import com.dragon.manage.dao.IUserInfoDAO;
import com.dragon.manage.service.IManageFinanceService;
import com.dragon.manage.util.ManageUtil;
import com.dragon.manage.util.RankUtil;
import com.dragon.manage.util.StaticData;
import com.dragon.manage.util.TimeUtil;

public class ManageFinanceServiceImpl implements IManageFinanceService{
	/** 比赛信息**/
	private IMatchInfoDAO matchInfoDAO;
	
	/** 战队信息*/
	private ITeamInfoDAO teamInfoDAO;

	/** 竞猜信息 */
	private IAmusementInfoDAO amusementInfoDAO;
	
	/** 用户下注信息 */
	private IUserBetDAO userBetDAO;
	
	/** 用户信息 */
	private IUserInfoDAO userInfoDAO;
	
	/** 开盘类型 */
	private IAmusementTypeDAO amusementTypeDAO;
	
	/** 管理用户 */
	private IManageUserDAO manageUserDao;
	
	/**缓存信息 */
	private ICacheDao cacheDAO;
	
	private IActivityInfoDAO activityInfoDAO;
	
	private IActivityOptionDAO activityOptionDAO;
	
	private IUserActivitybetDAO userActivitybetDAO;
	
	public void setUserActivitybetDAO(IUserActivitybetDAO userActivitybetDAO) {
		this.userActivitybetDAO = userActivitybetDAO;
	}
	public void setActivityOptionDAO(IActivityOptionDAO activityOptionDAO) {
		this.activityOptionDAO = activityOptionDAO;
	}

	
	public void setActivityInfoDAO(IActivityInfoDAO activityInfoDAO) {
		this.activityInfoDAO = activityInfoDAO;
	}
	
	public ICacheDao getCacheDAO() {
		return cacheDAO;
	}

	public void setCacheDAO(ICacheDao cacheDAO) {
		this.cacheDAO = cacheDAO;
	}
	public IMatchInfoDAO getMatchInfoDAO() {
		return matchInfoDAO;
	}

	public void setMatchInfoDAO(IMatchInfoDAO matchInfoDAO) {
		this.matchInfoDAO = matchInfoDAO;
	}

	public ITeamInfoDAO getTeamInfoDAO() {
		return teamInfoDAO;
	}

	public void setTeamInfoDAO(ITeamInfoDAO teamInfoDAO) {
		this.teamInfoDAO = teamInfoDAO;
	}

	public IAmusementInfoDAO getAmusementInfoDAO() {
		return amusementInfoDAO;
	}

	public void setAmusementInfoDAO(IAmusementInfoDAO amusementInfoDAO) {
		this.amusementInfoDAO = amusementInfoDAO;
	}

	public IUserBetDAO getUserBetDAO() {
		return userBetDAO;
	}

	public void setUserBetDAO(IUserBetDAO userBetDAO) {
		this.userBetDAO = userBetDAO;
	}

	public IUserInfoDAO getUserInfoDAO() {
		return userInfoDAO;
	}

	public void setUserInfoDAO(IUserInfoDAO userInfoDAO) {
		this.userInfoDAO = userInfoDAO;
	}

	public IAmusementTypeDAO getAmusementTypeDAO() {
		return amusementTypeDAO;
	}

	public void setAmusementTypeDAO(IAmusementTypeDAO amusementTypeDAO) {
		this.amusementTypeDAO = amusementTypeDAO;
	}

	public IManageUserDAO getManageUserDao() {
		return manageUserDao;
	}

	public void setManageUserDao(IManageUserDAO manageUserDao) {
		this.manageUserDao = manageUserDao;
	}

	@Override
	public JSONObject getBeginMatchList(int type,int page) throws Exception {
		
		JSONObject reJs = new JSONObject(); 
		List<MatchInfo> list = matchInfoDAO.getMatchInfoListByType(type,page,
				StaticData.AmusementStatus.first_result_check.getType(),type);
		JSONArray jsList = new JSONArray();
		for(MatchInfo tmp:list){
			jsList.add(ManageUtil.getMatchPage(tmp, teamInfoDAO));
		}
		reJs.put("matchInfoList", jsList);
		reJs.put(StaticData.reKey.total.getKey(), matchInfoDAO.getMatchCountByType(type, 
				StaticData.AmusementStatus.first_result_check.getType()));
		return reJs;
	}

	@Override
	public JSONObject settleMatch(int matchId,int settleType) throws Exception {
		JSONObject reJs = new JSONObject(); 
		/*
		 * 1根据id获取比赛
		 * 2 根据比赛获取下注项目id列表
		 * 3 根据项目id列表获取玩家下注列表
		 * 4 根据玩家下注列表，取消相关下注操作
		 * 5 修改相关的竞猜状态和比赛状态
		 */
		//根据id获取比赛
		MatchInfo matchInfo = matchInfoDAO.getMatchInfoByID(matchId);
		if(matchInfo.getMatchStatus()!=StaticData.AmusementStatus.second_result_check.getType()){
			reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
			return reJs;
		}
		//根据比赛获取下注项目id列表
		List<AmusementInfo> amusementInfoList = amusementInfoDAO.getAmusementInfoListByMatchId(matchInfo.getId());
		for(AmusementInfo tmp:amusementInfoList){
			if(tmp.getMatchStatus()==StaticData.AmusementStatus.cancellation.getType()){
				//结算单个竞猜
				amusementInfoCancelSettle(tmp, StaticData.AmusementStatus.cancellation.getType());
			}else{
				amusementInfoCancelSettle(tmp, settleType);
			}

			matchInfo.setProfit(matchInfo.getProfit()+tmp.getProfit());
		}
		matchInfo.setMatchStatus(settleType);
		matchInfoDAO.updateMatchInfoDB(matchInfo);
		reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		return reJs;
	}
	
	/**
	 * 单个竞猜结算
	 * @param amusementInfo
	 * @param type
	 */
	private void amusementInfoCancelSettle(AmusementInfo amusementInfo,int type){
		//获取用户的下注列表
		List<UserBet> userBetList = userBetDAO.getUserBetList(amusementInfo.getId());
		for(UserBet tmp:userBetList){
			userSettle(tmp, type, amusementInfo);
		}
		amusementInfo.setMatchStatus(type);
		amusementInfo.setSettleDate(TimeUtil.getSysteCurTime());
		amusementInfoDAO.updateAmusementInfo(amusementInfo);
	}
	
	/**
	 * 单个用户结算
	 * @param userBet
	 * @param type
	 * @param amusementInfo
	 */
	private void userSettle(UserBet userBet,int type,AmusementInfo amusementInfo){
		//获取用户信息
		UserInfo userInfo = userInfoDAO.getUserInfoByID(userBet.getUserId());

		int profit = 0;
		if(type == StaticData.AmusementStatus.cancellation.getType()){
			profit = userBet.getBetAmount();
		}else{
			if(userBet.getBetType()==amusementInfo.getAmusementResult()){
				profit = (int) (userBet.getBetAmount()*userBet.getOdds());
			}
			userBet.setProfit(profit-userBet.getBetAmount());
		}
		//如果收益大于0则更新用户信息
		if(profit>0){
			userInfo.setProfitTotal(userInfo.getProfitTotal()+profit);
			userInfoDAO.updateUserMoney(userInfo, 
					profit, 
					StaticData.updateMoneyType.amusement_add.getType(),
					userBet.getId()+"");
			userBet.setBetStatus(StaticData.userBetSettleType.win.getType());
			RankUtil.addUserProfitRank(userInfo.getId(), userInfo.getProfitTotal());
		}else{
			userBet.setBetStatus(StaticData.userBetSettleType.fail.getType());
		}
		userBet.setSettleDate(TimeUtil.getSysteCurTime());
		userBetDAO.updateUserBet(userBet);
		/*
		 * 更新用戶勝率和今日盈亏
		 */
		cacheDAO.remonveUserProfitTotalCache(userBet.getUserId());
		cacheDAO.remonveUserWinRateCache(userBet.getUserId());
//		cacheDAO.getUserWinRate(userInfo.getId(), StaticData.getDataType.real_time.getType(),userBetDAO);
		//如果是白名单用户则不计算统计
		if(userInfo.getType()!=StaticData.userType.white.getType()){
			//计算每场的盈利
			amusementInfo.setProfit(amusementInfo.getProfit()+userBet.getBetAmount()-profit);
		}
	}
	/**
	 * 计算某个比赛的收益
	 * @param userBet
	 * @param type
	 * @param amusementInfo
	 */
	private void mathMatchProfit(MatchInfo matchInfo){
		// 根据比赛获取下注项目id列表
		List<AmusementInfo> amusementInfoList = amusementInfoDAO.getAmusementInfoListByMatchId(matchInfo.getId());
		int count = 0;
		for (AmusementInfo tmp : amusementInfoList) {
			// 结算单个竞猜
			count +=mathAmusementInfoProfit(tmp);
		}
		matchInfo.setProfit(count);
	}
	/**
	 * 计算某个竞猜的收益
	 * @param userBet
	 * @param type
	 * @param amusementInfo
	 */
	private int mathAmusementInfoProfit(AmusementInfo amusementInfo){
		//获取用户的下注列表
		List<UserBet> userBetList = userBetDAO.getUserBetList(amusementInfo.getId());
		int count = 0;
		for(UserBet tmp:userBetList){
			count += mathUserProfit(tmp, amusementInfo);
		}
		return count;
	}
	/**
	 * 计算单个用户的收益
	 * @param userBet
	 * @param type
	 * @param amusementInfo
	 */
	private int mathUserProfit(UserBet userBet,AmusementInfo amusementInfo){
		//获取用户信息
		int profit = 0;
		if(userBet.getBetType()==amusementInfo.getAmusementResult()){
			profit = (int) (userBet.getBetAmount()*userBet.getOdds());
		}
		//计算每场的盈利
		return userBet.getBetAmount()-profit;
	}
	
	@Override
	public JSONArray getAmusementInfoList(int matchId) throws Exception {
		List<AmusementInfo> list = amusementInfoDAO.getAmusementInfoListByMatchId(matchId);
		JSONArray reList = new JSONArray();
		for(AmusementInfo tmp:list){
			reList.add(ManageUtil.getAmusementInfoPage(tmp, amusementTypeDAO, matchInfoDAO, teamInfoDAO));
		}
		return reList;
	}
	
	@Override
	public MatchInfo getMatchInfoById(int id) throws Exception {
		return matchInfoDAO.getMatchInfoByID(id);
	}
	
	@Override
	public AmusementInfo getAmusementInfoById(int id) throws Exception {
		return amusementInfoDAO.getAmusementInfoByID(id);
	}

	@Override
	public int checkResult(int amusementInfoId, int amusementResult)
			throws Exception {
		AmusementInfo amusementInfo = amusementInfoDAO.getAmusementInfoByID(amusementInfoId);
		/*
		 * 结果不一致则返回
		 */
		if(amusementResult!=amusementInfo.getAmusementResult()){
			return StaticData.codeType.check_result_error.getType();
		}else if(amusementResult==StaticData.cancel){
			//下注作废
			amusementInfo.setMatchStatus(StaticData.AmusementStatus.cancellation.getType());
			amusementInfoDAO.updateAmusementInfoDB(amusementInfo);
		}else{
			//结果一致则更新该条状态
			amusementInfo.setMatchStatus(StaticData.AmusementStatus.second_result_check.getType());
			amusementInfoDAO.updateAmusementInfoDB(amusementInfo);
		}
		return StaticData.codeType.ok.getType();
	}

	@Override
	public int submitResult(int id,ManageUser manageUser) throws Exception {
		List<AmusementInfo> list = 
				amusementInfoDAO.getAmusementInfoListByMatchStatus(id,
				StaticData.AmusementStatus.first_result_check.getType());
		if(list==null||list.size()==0){
			MatchInfo tmp = matchInfoDAO.getMatchInfoByID(id);
			tmp.setMatchStatus(StaticData.AmusementStatus.second_result_check.getType());
			tmp.setSecondResult(manageUser.getId());
			//计算该比赛的亏盈状况
			mathMatchProfit(tmp);
			matchInfoDAO.updateMatchInfoDB(tmp);
			return StaticData.codeType.ok.getType();
		}else{
			return StaticData.codeType.not_affirm_finish.getType();
		}
	}

	@Override
	public int rejectResult(int id,ManageUser manageUser) throws Exception {
		/*
		 * 驳回结果，先查出比赛
		 * 再查出竞猜列表
		 * 修改竞猜列表状态
		 * 修改比赛状态
		 */
		//根据id获取比赛
		MatchInfo matchInfo = matchInfoDAO.getMatchInfoByID(id);
//		List<AmusementInfo> amusementInfoList = null;
		//判断状态是否是第一次结果录入了
		if(matchInfo.getMatchStatus()==StaticData.AmusementStatus.first_result_check.getType()){
			//根据比赛获取下注项目id列表
//			amusementInfoList = amusementInfoDAO.getAmusementInfoListByMatchId(matchInfo.getId());
			matchInfo.setMatchStatus(StaticData.AmusementStatus.putaway.getType());
			matchInfoDAO.updateMatchInfoDB(matchInfo);
		}
		
		return StaticData.codeType.ok.getType();
	}

	@Override
	public JSONObject getAwardDistributionList(int type, int page) throws Exception {
		JSONObject reJs = new JSONObject(); 
		List<MatchInfo> list = matchInfoDAO.getMatchInfoListByType(type,page,
				StaticData.AmusementStatus.second_result_check.getType(),type);
		JSONArray jsList = new JSONArray();
		for(MatchInfo tmp:list){
			jsList.add(ManageUtil.getAwardDistributionPage(tmp, teamInfoDAO,manageUserDao));
		}
		reJs.put("matchInfoList", jsList);
		if(page!=StaticData.no_param){
			reJs.put(StaticData.reKey.total.getKey(), matchInfoDAO.getMatchCountByType(type, StaticData.AmusementStatus.second_result_check.getType()));
		}
		return reJs;
	}

	
	@Override
	public JSONObject getWhiteUserList(int page) throws Exception {
		JSONObject reJs = new JSONObject();
		JSONArray reArray = new JSONArray();
		JSONObject jsTmp = null;
		List<UserInfo> userList = userInfoDAO.getUserInfoList(StaticData.userType.white.getType(), page);
		for(UserInfo tmp:userList){
			jsTmp = ManageUtil.getWhiteUserPage(tmp);
			reArray.add(jsTmp);
		}
		reJs.put("userList", reArray);
		if(page!=StaticData.no_param){
			reJs.put(StaticData.reKey.total.getKey(), userInfoDAO.getUserInfoCount(StaticData.userType.white.getType()));
		}
		return reJs;
	}

	@Override
	public JSONObject addWhiteUser(int userId) throws Exception {
		JSONObject reJs = new JSONObject();
		UserInfo user = userInfoDAO.getUserInfoByID(userId);
		user.setType(StaticData.userType.white.getType());
		userInfoDAO.updateUserInfo(user);
		userInfoDAO.updateUserInfoDB(user);
		return reJs;
	}

	@Override
	public JSONObject addWhiteUserMoney(int userId,int amount,int manageId) throws Exception {
		JSONObject reJs = new JSONObject();
		UserInfo user = userInfoDAO.getUserInfoByID(userId);
		if(user.getType()==StaticData.userType.white.getType()){
			userInfoDAO.updateUserMoney(userId, 
					amount, 
					StaticData.updateMoneyType.white_add.getType(),
					manageId+"");
			reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		}else{
			reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.not_white.getType());
		}
		return reJs;
	}

	@Override
	public JSONObject getUserInfo(int userId) throws Exception {
		JSONObject reJs = new JSONObject();
		UserInfo userInfo = userInfoDAO.getUserInfoByID(userId);
		if(userInfo == null){
			reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
		}else {
			reJs.put(StaticData.reKey.userInfo.getKey(), userInfo);
			reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		}
		return reJs;
	}
	
	@Override
	public JSONObject getActiveList(int page) throws Exception {
		JSONObject reJ = new JSONObject();
		JSONArray jsa = new JSONArray();
		List<ActivityInfo> list = activityInfoDAO.getActivityInfoList(
				StaticData.AmusementStatus.first_result_check.getType(), 
				false, StaticData.no_param, page);
		if(list!=null){
			for(ActivityInfo tmp:list){
				jsa.add(activityInfoToPage(tmp));
			}
		}
		reJ.put(StaticData.reKey.rows.getKey(),jsa);
		reJ.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		if(page!=StaticData.no_param){
			reJ.put(StaticData.reKey.total.getKey(),
					activityInfoDAO.getActivityInfoCount(
							StaticData.AmusementStatus.first_result_check.getType(), 
							false, StaticData.no_param));
		}
		return reJ;
	}

	private JSONObject activityInfoToPage(ActivityInfo activity){
		JSONObject reJ = new JSONObject();
		reJ.put("id", activity.getId());
		reJ.put("title", activity.getActivityTitle());
		reJ.put("activityImage", activity.getActivityImage());
		reJ.put("footImage", activity.getFootImage());
		reJ.put("endDate", TimeUtil.getDateFormat(activity.getEndDate()));
		reJ.put("betTimes", activity.getRelBetTimes());
		reJ.put("betMoney", activity.getNeedGold()*activity.getRelBetTimes());
		return reJ;
	}
	
	@Override
	public JSONObject getActivityOptionList(int activityId) throws Exception {
		JSONObject reJ = new JSONObject();
		List<ActivityOption> list = activityOptionDAO.getActivityOptionList(activityId);
		JSONArray reArray = new JSONArray();
		if(list!=null){
			for(ActivityOption tmp:list){
				reArray.add(activityOptionToPage(tmp));
			}
		}
		reJ.put(StaticData.reKey.rows.getKey(),reArray);
		reJ.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		return reJ;
	}
	
	private JSONObject activityOptionToPage(ActivityOption tmp){
		JSONObject reJ = new JSONObject();
		reJ = new JSONObject();
		reJ.put("opptionId", tmp.getId());
		reJ.put("nameType", tmp.getNameType());
		reJ.put("title", tmp.getTitle());
		reJ.put("amusementName", tmp.getAmusementName());
		reJ.put("oddsInfoType", tmp.getOddsInfoType());
		reJ.put("oddsInfo",getActiveOddsInfoIType(tmp.getOddsInfo()));
/*		if(tmp.getAmusementResult()!=StaticData.no_param){
			reJ.put("amusementResult",tmp.getAmusementResult());
		}*/
		return reJ;
	}
	
	private JSONArray  getActiveOddsInfoIType(String oddsInfo){
		String[] tmps = oddsInfo.split(StaticData.second_split);
		String[] tmpT = null;
		JSONObject reTmp = null;
		JSONArray reArray = new JSONArray();
		for(String tmp:tmps){
			reTmp = new JSONObject();
			tmpT = tmp.split(StaticData.first_split);
			if(tmpT.length>0){
				reTmp.put("oddsInfoId", tmpT[0]);
			}
			if(tmpT.length>1){
				reTmp.put("oddsInfoStr", tmpT[1]);
			}
			if(tmpT.length>2){
				reTmp.put("oddsNum", tmpT[2]);
			}
			
			reArray.add(reTmp);
		}
		return reArray;
	}


	@Override
	public JSONObject updateActivityOption(int id, int resultId)
			throws Exception {
		JSONObject reJ = new JSONObject();
		ActivityOption option = activityOptionDAO.getActivityOptionByID(id);
		if(option==null||option.getMatchStatus()!=StaticData.AmusementStatus.first_result_check.getType()){
			reJ.put(StaticData.reKey.code.getKey(), StaticData.codeType.not_exist.getType());
			return reJ;
		}
		if(resultId==StaticData.no_param||resultId!=option.getAmusementResult()){
			reJ.put(StaticData.reKey.code.getKey(), StaticData.codeType.check_result_error.getType());
			return reJ;
		}
		
		option.setMatchStatus(StaticData.AmusementStatus.second_result_check.getType());
		activityOptionDAO.updateActivityOption(option);
		reJ.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		return reJ;
	}

	@Override
	public JSONObject overActivityOption(int activityId) throws Exception {
		JSONObject reJ = new JSONObject();
		ActivityInfo activityInfo = activityInfoDAO.getActivityInfoByID(activityId);
		if(activityInfo==null){
			reJ.put(StaticData.reKey.code.getKey(), StaticData.codeType.not_exist.getType());
			return reJ;
		}
		List<ActivityOption> list = activityOptionDAO.getActivityOptionList(activityId);
		if(list==null||list.size()<=0){
			reJ.put(StaticData.reKey.code.getKey(), StaticData.codeType.not_exist2.getType());
			return reJ;
		}
		boolean b = true;
		for(ActivityOption tmp:list){
			if(tmp.getMatchStatus()!=StaticData.AmusementStatus.second_result_check.getType()){
				b = false;
				break;
			}
		}
		if(b){
			activityInfo.setMatchStatus(StaticData.AmusementStatus.second_result_check.getType());
			activityInfoDAO.updateActivityInfo(activityInfo);
			reJ.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
			return reJ;
		}else{
			reJ.put(StaticData.reKey.code.getKey(), StaticData.codeType.not_affirm_finish.getType());
			return reJ;
		}
	}


	@Override
	public JSONObject activityAwardList(int page) throws Exception {
		JSONObject reJ = new JSONObject();
		
		List<ActivityInfo> list = activityInfoDAO.getActivityInfoList(
				StaticData.AmusementStatus.second_result_check.getType(), 
				false, StaticData.no_param, page);
		if(list!=null){
			JSONArray jsa = new JSONArray();
			JSONObject jsTmp = null;
			for(ActivityInfo tmp:list){
				jsTmp = new JSONObject();
				/*
				 * 中奖人数
				 * 领奖人数
				 */
				List<ActivityOption> olist = activityOptionDAO.getActivityOptionList(tmp.getId());
				StringBuilder sb = new StringBuilder();
				for(ActivityOption otmp:olist){
					sb.append(otmp.getId()+StaticData.first_split+otmp.getAmusementResult()+StaticData.second_split);
				}
				String result = sb.substring(0, sb.length()-1);
				int allWinCount = userActivitybetDAO.count(StaticData.no_param,tmp.getId(),
						StaticData.no_param, result,StaticData.no_param);
				int getWin = userActivitybetDAO.count(StaticData.no_param,tmp.getId(),
						StaticData.no_param, result,StaticData.userBetSettleType.win.getType());
				jsTmp.put("id",tmp.getId());
				jsTmp.put("title",tmp.getActivityTitle());
				jsTmp.put("allWinCount",allWinCount);
				jsTmp.put("getWin",getWin);
				jsTmp.put("notGet",allWinCount-getWin);
				jsa.add(jsTmp);
			}
			reJ.put(StaticData.reKey.rows.getKey(),jsa);
		}
		reJ.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		if(page!=StaticData.no_param){
			reJ.put(StaticData.reKey.total.getKey(),
					activityInfoDAO.getActivityInfoCount(
							StaticData.AmusementStatus.first_result_check.getType(), 
							false, StaticData.no_param));
		}
		return reJ;
	}
	@Override
	public JSONObject guessWinnerList(int activeId, int page) throws Exception {
		JSONObject reJ = new JSONObject();
		List<ActivityOption> olist = activityOptionDAO.getActivityOptionList(activeId);
		StringBuilder sb = new StringBuilder();
		for(ActivityOption otmp:olist){
			sb.append(otmp.getId()+StaticData.first_split+otmp.getAmusementResult()+StaticData.second_split);
		}
		String result = sb.substring(0, sb.length()-1);
		List<UserActivitybet> list = userActivitybetDAO.getUserActivitybetList(StaticData.no_param, activeId,
				StaticData.no_param, result, StaticData.no_param, page);
		if(list!=null){
			JSONArray jsa = new JSONArray();
			JSONObject jsTmp = null;
			UserInfo userTmp = null;
			for(UserActivitybet tmp:list){
				jsTmp = new JSONObject();
				userTmp= userInfoDAO.getUserInfoByID(tmp.getUserId());
				jsTmp.put("id",tmp.getId());
				jsTmp.put("userId", tmp.getUserId());
				jsTmp.put("name", userTmp.getNickname());
				jsTmp.put("CDK", tmp.getUserRemark());
				jsTmp.put("status", tmp.getBetStatus());
				jsa.add(jsTmp);
			}
			reJ.put(StaticData.reKey.rows.getKey(),jsa);
		}
		reJ.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		
		if(page!=StaticData.no_param){
			reJ.put(StaticData.reKey.total.getKey(),
					userActivitybetDAO.count(StaticData.no_param, activeId,
							StaticData.no_param, result, StaticData.no_param));
		}
		return reJ;
	}
	@Override
	public JSONObject updateActivityWinner(int userBetId, String CDK)
			throws Exception {
		JSONObject reJ = new JSONObject();
		UserActivitybet userbet = userActivitybetDAO.getUserActivitybetByID(userBetId);
		if(userbet==null){
			reJ.put(StaticData.reKey.code.getKey(), StaticData.codeType.not_exist.getType());
			return reJ;
		}
		if(!CDK.equals(userbet.getUserRemark())){
			reJ.put(StaticData.reKey.code.getKey(), StaticData.codeType.check_result_error.getType());
			return reJ;
		}
		userbet.setBetStatus(StaticData.userBetSettleType.OVER.getType());
		userActivitybetDAO.updateUserActivitybet(userbet);
		reJ.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		return reJ;
	}

}
